// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ObjectDetectionModelAgent.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Robotics.Vision.Visualization
{
    using System;
    using System.Runtime.Serialization;
    using System.Windows;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using Microsoft.Robotics.Numerics;
    using Microsoft.Robotics.Runtime;
    using Microsoft.Robotics.Vision.ObjectDetection;
    using Microsoft.Robotics.Vision.Runtime;
    using Microsoft.Robotics.Vision.Visualization.Cameras.ObjectDetection;
    using Microsoft.Robotics.Visualization;

    /// <summary>
    /// This model agent class prepares the object detection result for visualization.
    /// </summary>
    [DataContract]
    public sealed class ObjectDetectionModelAgent : ModelAgent<KinectObjectDetectorResultIRMessage, ObjectDetectionModel>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ObjectDetectionModelAgent" /> class
        /// </summary>
        /// <param name="name">Agent name</param>
        /// <param name="producer">The name of the agent we subscribe to.</param>
        /// <param name="maxUpdateIntervalMilliseconds">The interval on which to issue data binding updates to the UI</param>
        public ObjectDetectionModelAgent(
            string name, 
            IProducer<KinectObjectDetectorResultIRMessage> producer, 
            int maxUpdateIntervalMilliseconds)
            : base(name, producer.Name, maxUpdateIntervalMilliseconds)
        {
        }

        /// <summary>
        /// Updates the model data with the new state.
        /// </summary>
        /// <param name="state">The new state from the upstream agent.</param>
        /// <returns>true if the model was updated, false otherwise</returns>
        protected override bool UpdateModel(KinectObjectDetectorResultIRMessage state)
        {
            ImageFrame<ushort> image = DepthImageVisualizationUtilities.AddCrossToImageAtLocations(state.IRframe, state.State);

            string poseInfo = string.Empty;
            for (int i = 0; i < state.State.Length; ++i)
            {
                ObjectPoseEstimate e  = state.State[i];
                Vector3 v = e.CameraPixelPose.Position;
                poseInfo += string.Format("{0} Conf {1:F3} Pos {2:F0} {3:F0} {4:F0}\n", e.ManipulatedObjectDescription.Description, e.Confidence, v.X, v.Y, v.Z);
            }

            this.Model.PoseInformationText = poseInfo;

            this.Model.VideoFrame = new VideoFrame()
            {
                Data = image.ImageData,
                Size = new System.Drawing.Size(state.IRframe.Width, state.IRframe.Height),
                Format = SimplePixelFormat.Gray16,
                Stride = state.IRframe.Width * 2
            };

            return true;
        }
    }
}
